package za.co.dylanjaneke.airdownloader.model.vo
{
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.SQLEvent;
	
	/**
	 * Author: Dylan Janeke.
	 */
	[Bindable]
	public class SettingsVO implements DBVO
	{	
		public var username: String;
		public var password: String;
		public var id:Number;
		
		public function SettingsVO()
		{
			
		}
		
		public function get createQuery(): String
		{
			return "CREATE TABLE IF NOT EXISTS ad_cfg (id INTEGER PRIMARY KEY AUTOINCREMENT, u_name TEXT, p_word TEXT);";
		}
		
		public static function valueOf(event:SQLEvent): SettingsVO
		{
			var statement:SQLStatement= event.currentTarget as SQLStatement;
			var result:SQLResult= statement.getResult();
			if(result==null|| result.data==null)
				return null;
			
			var vo:SettingsVO= new SettingsVO();
			
			var row:Object= result.data[0];
			vo.id = row.id;
			vo.username= row.u_name;
			vo.password= row.p_word;
			
			return vo;
		}
		
		public static function createInsert(vo:SettingsVO, statement:SQLStatement): SQLStatement
		{
			statement.text = "INSERT INTO ad_cfg (u_name,p_word) VALUES (:username,:password)";
			statement.parameters[":username"]= vo.username;
			statement.parameters[":password"]= vo.password;
			
			return statement;
		}
		
		public static function createUpdate(vo:SettingsVO, statement:SQLStatement): SQLStatement
		{
			
			statement.text = "UPDATE ad_cfg SET u_name = :username, p_word = :password WHERE id = :id";
			statement.parameters[":id"]= vo.id;
			statement.parameters[":username"]= vo.username;
			statement.parameters[":password"]= vo.password;
			
			return statement;
		}
	}
}